﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SceneResources : MonoBehaviour
{
    private static SceneResources instance = null;
    private List<TextAsset> textAssetList = new List<TextAsset>();
    public static SceneResources mInstance
    {
        get
        {
            if (instance == null)
            {
                instance = GameObject.FindObjectOfType<SceneResources>();
                if (instance == null)
                {
                    Debug.LogError("这个脚本应该先挂在场景隐藏空物体上，然后把用到的Resouces下的资源作为子物体。");
                }
            }
            return instance;
        }
    }

    void OnApplicationQuit() { if (instance != null) instance = null; }

    void Awake()
    {    
        instance = this;
    }
    void Start()
    {
        GetTextAssetList();
    }

    //请求赋值，
    void GetTextAssetList()
    {
        if (textAssetList.Count == 0)
            transform.Find("_AddTextAsset").gameObject.SendMessage("GetAssetList");
    }
    //收到赋值
    void SetTextAssetList(List<TextAsset> textAsset)
    {
        textAssetList = textAsset;
    }

    public Object Load(string pathName)
    {
        return transform.Find(pathName).gameObject;
    }

    public GameObject LoadGameObj(string pathName)
    {
        return transform.Find(pathName).gameObject;
    }

    public Material LoadMaterial(string name)
    {
        return transform.Find(name).GetComponent<MeshRenderer>().material;
    }

    public Sprite LoadSprite(string name)
    {
        Sprite oResSprite = null;
        if (transform.Find(name).GetComponent<Image>() != null)
        {
            oResSprite = transform.Find(name).GetComponent<Image>().sprite;
        }
        else if (transform.Find(name).GetComponent<SpriteRenderer>() != null)
        {
            oResSprite = transform.Find(name).GetComponent<SpriteRenderer>().sprite;
        }

        return oResSprite;
    }

    public AudioClip LoadAudioClip(string name)
    {
        return transform.Find(name).GetComponent<AudioSource>().clip;
    }

    public TextAsset LoadTextAsset(int index)
    {
        GetTextAssetList();
        return textAssetList[index];
    }
    public List<TextAsset> LoadTextAsset()
    {
        GetTextAssetList();
        return textAssetList;
    }
}

